packages = c( 'sf','tmap','tidyverse','lubridate',
              'clock','sftime','rmarkdown')
for (p in packages){
  if(!require(p, character.only = T)){
    install.packages(p)
  }
  library(p,character.only = T)
}
## Loading required package: sf
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
## Loading required package: tmap
## Loading required package: tidyverse
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## Loading required package: lubridate
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
## Loading required package: clock
## 
## Attaching package: 'clock'
## The following object is masked from 'package:lubridate':
## 
##     as_date
## Loading required package: sftime
## Loading required package: rmarkdown
school=read_sf('data/Schools.csv',options='GEOM_POSSIBLE_NAMES=location')

pub=read_sf('data/Pubs.csv',options='GEOM_POSSIBLE_NAMES=location')

apartment=read_sf('data/Apartments.csv',options='GEOM_POSSIBLE_NAMES=location')

restaurant=read_sf('data/Restaurants.csv',options='GEOM_POSSIBLE_NAMES=location')

employer=read_sf('data/Employers.csv',options='GEOM_POSSIBLE_NAMES=location')

building=read_sf('data/Buildings.csv',options='GEOM_POSSIBLE_NAMES=location')
tmap_mode('view')
## tmap mode set to interactive viewing
tm_shape(building)+
  tm_polygons(col="grey60",
  size=1,
  border.col='black',
  border.lwd=1)
## Warning: The projection of the shape object building is not known, while it
## seems to be projected.
## Warning: Current projection of shape building unknown and cannot be determined.
tmap_mode('plot')
## tmap mode set to plotting
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(building)+
tm_polygons(col = "grey60",
           size = 1,
           border.col = "black",
           border.lwd = 1) +
tm_shape(employer) +
  tm_dots(col = "red")
## Warning: The projection of the shape object building is not known, while it
## seems to be projected.
## Warning: Current projection of shape building unknown and cannot be determined.
## Warning: Current projection of shape employer unknown and cannot be determined.
logs=read_sf('C:/Users/65873/Downloads/Visual Analytics/VAST-Challenge-2022/Datasets/Activity Logs/ParticipantStatusLogs1.csv',options='GEOM_POSSIBLE_NAMES=currentLocation')
logs_selected<-logs%>% 
  mutate(Timestamp=date_time_parse(timestamp,
                                   zone="",
                                format="%Y-%m-%dT&H:%M:%S"))
logs_selected<-logs_selected %>%
  mutate(day=get_day(Timestamp))%>%
  filter(currentMode=='Transport')
write_rds(logs_selected,"logs_selected.rds")
log_selected<-read_rds('logs_selected.rds')
hex <- st_make_grid(building, #define the bbox 
                    cellsize=100, #dimention:100x100
                    square=FALSE) %>%
  st_sf() %>%
  rowid_to_column('hex_id')
points_in_hex <- st_join(log_selected, 
                        hex, 
                        join=st_within) %>%
  st_set_geometry(NULL) %>%
  count(name='pointCount', hex_id)
head(points_in_hex)
## # A tibble: 6 x 2
##   hex_id pointCount
##    <int>      <int>
## 1    169         35
## 2    212         56
## 3    225         21
## 4    226         94
## 5    227         22
## 6    228         45
hex_combined <- hex %>%
  left_join(points_in_hex, 
            by = 'hex_id') %>%
  replace(is.na(.), 0)
tm_shape(hex_combined %>%
           filter(pointCount > 0))+
  tm_fill("pointCount",
          n = 8,
          style = "quantile") +
  tm_borders(alpha = 0.1)
## Warning: The projection of the shape object hex_combined %>% filter(pointCount >
## 0) is not known, while it seems to be projected.
## Warning: Current projection of shape hex_combined %>% filter(pointCount > 0)
## unknown and cannot be determined.
logs_path <- log_selected %>%
  group_by(participantId, day) %>%
  summarize(m = mean(Timestamp), 
            do_union=FALSE) %>%
  st_cast("LINESTRING")
## `summarise()` has grouped output by 'participantId'. You can override using the
## `.groups` argument.
print(logs_path)
## Simple feature collection with 1011 features and 3 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: -4616.828 ymin: 35.4377 xmax: 2630 ymax: 7836.546
## CRS:           NA
## # A tibble: 1,011 x 4
## # Groups:   participantId [1,011]
##    participantId   day m                                         currentLocation
##    <chr>         <int> <dttm>                                       <LINESTRING>
##  1 0                NA NA     (-2721.353 6862.861, -2689.275 6644.643, -3004.15~
##  2 1                NA NA     (-1531.133 5597.244, -1863.284 5825.793, -2282.97~
##  3 10               NA NA     (1816.355 5215.958, 2162.077 5261.447, 2165.435 4~
##  4 100              NA NA     (-4579.08 7267.63, -4219.829 7379.372, -4186.85 6~
##  5 1000             NA NA     (141.3757 799.1239, 256.0645 1059.237, 231.1388 1~
##  6 1001             NA NA     (930.477 2579.158, 664.3738 2442.666, 601.66 2038~
##  7 1002             NA NA     (-2252.384 2675.09, -1993.155 2773.045, -1968.088~
##  8 1003             NA NA     (-4254.372 5254.282, -3986.464 5442.484, -3697.99~
##  9 1004             NA NA     (-2464.407 3264.935, -1969.175 3255.297, -1935.29~
## 10 1005             NA NA     (-4167.108 5249.201, -4025.708 5621.071, -3790.19~
## # ... with 1,001 more rows
logs_path_selected<-logs_path %>% 
  filter(participantId==0)
tmap_mode("plot")
## tmap mode set to plotting
tm_shape(building)+
  tm_polygons(col="grey60",
              size=1,
              border.col='black',
              border.lwd=1)+
  tm_shape(logs_path_selected)+
  tm_lines(col='blue')
## Warning: The projection of the shape object building is not known, while it
## seems to be projected.
## Warning: Current projection of shape building unknown and cannot be determined.
## Warning: Current projection of shape logs_path_selected unknown and cannot be
## determined.

tmap_mode('plot')
## tmap mode set to plotting